/*
 * @Descripttion: 护士功能模块
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-04-28 15:01:19
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-07 18:40:23
 */
const koaRouter = require('koa-router')
const router = new koaRouter()


const online = require('../middleware/nurse/online')
const login = require('../middleware/nurse/login')
const get_buy_medicine = require('../middleware/nurse/get_buy_medicine')
const put_pack = require('../middleware/nurse/put_pack')
const put_deal = require('../middleware/nurse/put_deal')
const get_info = require('../middleware/nurse/get_info')
const get_medicine_lib_info = require('../middleware/nurse/get_medicine_lib_info')
const get_medicine_lib_remove_info = require('../middleware/nurse/get_medicine_lib_remove_info')
const create_medicine_lib = require('../middleware/nurse/create_medicine_lib')
const create_doctor_medicine_lib = require('../middleware/nurse/create_doctor_medicine_lib')

const { NURSE } = require('../lib/Identity')
const checkIdentityByBody = require('../middleware/common/checkIdentityByBody')
const checkIdExistByBody = require('../middleware/common/checkIdExistByBody')
const error = require('../middleware/common/error')
const nurse = require('../middleware/user/nurse')
const pop = require('../middleware/doctor_medicine_lib/pop')
const push = require('../middleware/doctor_medicine_lib/push')

router.prefix('/nurse')

/** 护士登录 */
router.post(['/login'], async function (ctx, next) {
    ctx.checkBody('name').notEmpty().len(1)
    ctx.checkBody('pwd').notEmpty().len(6).md5()
    await next();
}, error, login)

/** 护士将药品分发给医生 */
router.post('/pop', nurse, async function (ctx, next) {
    ctx.checkBody('doctor').notEmpty().isInt().toInt();
    ctx.checkBody('medicine_lib_id').notEmpty().isInt().toInt();
    ctx.checkBody('out_amount').notEmpty().isInt().toInt();
    ctx.checkBody('memo').optional().default(null);
    await next();
}, error, checkIdentityByBody('doctor', NURSE), checkIdExistByBody('doctor_medicine_lib', 'medicine_lib_id'), pop)

/** 护士将药品入库 */
router.post(['/push', '/create_medicine_lib'], online, async function (ctx, next) {

    ctx.checkBody('medicine_name').notEmpty().len(2, 50);
    ctx.checkBody('lot_identification_mark').notEmpty().len(2, 50);
    ctx.checkBody('specification').notEmpty().len(2, 50);// 产品规格
    ctx.checkBody('sort_id').notEmpty().isInt(); // 外键 类别表
    ctx.checkBody('dose').notEmpty().isInt(); // 外键 剂量表
    ctx.checkBody('amount').notEmpty().isInt();
    ctx.checkBody('out_amount').notEmpty().isInt();
    ctx.checkBody('unit_price').notEmpty().isFloat();
    ctx.checkBody('sell_price').notEmpty().isFloat();
    ctx.checkBody('manufacture_date').notEmpty().isInt().toInt(); // 时间戳
    ctx.checkBody('expiration_date').notEmpty().isInt().toInt(); // 时间戳
    ctx.checkBody('expiration_flag').notEmpty().toBoolean();
    ctx.checkBody('manufacturer').notEmpty().isInt(); //外键 厂家表
    ctx.checkBody('provider').notEmpty().len(2, 50);
    ctx.checkBody('memo').optional().default(null);

    ctx.request.body.storage_time = Date.now(); // 入库日期
    ctx.request.body.conservator = ctx.session.nurse.id; // 药房管理员

    await next();
}, error, create_medicine_lib)

/** 护士获取患者的买药订单 */
router.get(['/buy_medicine', '/buy_medicine_list', '/get_buy_medicine'], online, get_buy_medicine)

/** 护士修改买药订单为(打包完成/等待患者拿药) */
router.put(['/buy_medicine', '/pack_buy_medicine'], online, async function (ctx, next) {
    ctx.checkBody('id').notEmpty()
    ctx.checkBody('memo').default('药品已打包完毕，请尽快取药。')
    await next();
}, error, put_pack)

/** 护士修改买药订单为(打包完成/等待患者拿药) */
router.put(['/deal_buy_medicine'], online, async function (ctx, next) {
    ctx.checkBody('id').notEmpty()
    ctx.checkBody('memo').default('交易完成，给个好评呗~')
    await next();
}, error, put_deal)

/** 护士获取自己的工作信息 */
router.get(['/get_info'], online, get_info)

/** 护士获取入库辅助信息，方便自己填写入库信息 */
router.get(['/get_medicine_lib_info'], online, get_medicine_lib_info)
/** 护士获取出库辅助信息，方便自己填写出库信息 */
router.get(['/get_medicine_lib_remove_info'], online, get_medicine_lib_remove_info)

/** 护士给医师拿药 */
router.post(['/doctor_medicine_lib'], online, async function (ctx, next) {
    ctx.checkBody('medicine_lib_id').notEmpty().isInt();
    ctx.checkBody('amount').notEmpty().isInt();
    ctx.checkBody('doctor').notEmpty().isInt();
    ctx.checkBody('memo').default('无');

    ctx.request.body.out_amount = ctx.request.body.amount;
    ctx.request.body.conservator = ctx.session.nurse.id;
    ctx.request.body.out_time = Date.now();
    await next()
}, error, create_doctor_medicine_lib)

module.exports = router